'''
收藏夹测试用例
添加到收藏夹 /登录用户可用  加入购物车 删除
'''
import unittest
from common.Base import Base, open_browser
from Page.shopping_process_page import shopping_process
from common.databaseTool import DatabaseTool
from selenium.webdriver.common.by import By
import ddt
from common.openExcel import Open_excel
import os
from Page.shoppingcart_page import Shopingcart_page
import hashlib

# 登录页面url
url = "http://localhost:8080/ecshop/user.php"


class Shopingcart_case(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.data = DatabaseTool("168168", "ecshop_db", port=3308)
        sql = f"insert into ecs_users (user_name,password) values (,'test','{hashlib.md5(b'111111').hexdigest()}')"
        cls.data.execute_one(sql)

    @classmethod
    def tearDownClass(cls):
        sql = "delete from ecs_users where user_name like '%test%'"
        cls.data.execute_one(sql)

    def setUp(self):
        # 每一个用例执行前执行 准备测试数据
        op = open_browser()
        self.shop = shopping_process(op)
        self.shop.open_url(url)
        self.shop.input_username("test")
        self.shop.input_password("111111")
        self.shop.click_submit()
        self.shop.click_homepage()

    def tearDown(self):
        # 每一个用例执行后执行 清除环境
        # 关闭浏览器
        self.shop.quit()
        pass

    def test_shopping_favorites_041(self):
        # 点击用户中心
        self.shop.click_usercenter()
        # 判断是否进入登录用户页面
        try:
            ys_value = self.shop.get_element_text((By.CSS_SELECTOR, "tr:nth-child(4) img"))
            self.assertEqual(ys_value, "欢迎您回到 ECSHOP！", msg="用例执行失败未在登录/注册页面")
        except:
            self.assertTrue(True)

    def test_shopping_favorites_042(self):
        # 点击智能相机商品
        self.shop.click_Smartcamera()
        # 点击加入收藏
        self.shop.click_add_the_favorites()
        # 点击用户中心
        self.shop.click_usercenter()
        # 点击收藏夹
        self.shop.click_mycollection()
        # 判断是否是在我的收藏 页面
        self.assertEqual(self.shop.is_mycollection(), "我的收藏", msg="用例执行失败,未在我的收藏夹页面")

    def test_shopping_favorites_043(self):
        # 点击视频
        self.shop.click_video()
        # 点击加入收藏
        self.shop.click_add_the_favorites()
        #获取弹框点击确定
        # self.shop.click_window()
        # 点击用户中心
        self.shop.click_usercenter()
        # 点击收藏夹
        self.shop.click_mycollection()
        # 点击加入购物车
        self.shop.click_add_shoppingcart()
        # 断言购物车中是否有该商品
        self.assertEqual(self.shop.in_shopping_cart(), "视频", msg="用例执行失败商品不在购物车内")

    def test_shopping_favorites_044(self):
        # 点击视频
        self.shop.click_video()
        # 点击加入收藏
        self.shop.click_add_the_favorites()
        # 点击用户中心
        self.shop.click_usercenter()
        # 点击收藏夹
        self.shop.click_mycollection()
        # 点击删除
        self.shop.click_del()
        try:
            self.shop.click_window()
            # 获取到弹框断言
            self.assertTrue(True)
        except:
            # 未获取到弹框断言
            self.assertTrue(False, "未找到弹框,或没有弹框")

    def test_shopping_favorites_045(self):
        # 点击视频
        self.shop.click_video()
        # 点击加入收藏
        self.shop.click_add_the_favorites()
        # 点击用户中心
        self.shop.click_usercenter()
        # 点击收藏夹
        self.shop.click_mycollection()
        # 断言收藏夹中是否有该商品
        try:
            self.shop.select_goods("视频")
            self.assertTrue(True)
        except:
            self.assertTrue(False)

    def test_shopping_favorites_046(self):
        # 点击视频
        self.shop.click_video()
        # 点击加入收藏
        self.shop.click_add_the_favorites()
        # 点击用户中心
        self.shop.click_usercenter()
        # 点击收藏夹
        self.shop.click_mycollection()
        # 点击删除
        self.shop.click_del()
        # 获取弹框点击确定
        self.shop.click_window()
        # 断言收藏夹是否还有该商品
        try:
            self.shop.select_goods("视频")
            self.assertTrue(True)
        except:
            self.assertTrue(False)

